/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.activity;

import android.os.Bundle;
import android.support.annotation.StringRes;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.BaseUIFragment;
import com.ouertech.android.agm.lib.ui.base.FragmentTab;
import com.ouertech.android.agm.lib.ui.base.FragmentTabAdapter;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.User;
import com.simon.mengkou.data.enums.ESex;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.fragment.UserFollowFragment;
import com.simon.mengkou.ui.fragment.UserTopicFragment;
import com.simon.mengkou.ui.fragment.UserVideoFragment;
import com.simon.mengkou.ui.view.AvatarSimpleDraweeView;
import com.simon.mengkou.ui.view.FollowButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/18.
 * desc    : 用户详情界面
 */
public class UserActivity extends BaseTopActivity{
    //头像
    @Bind(R.id.user_id_avatar)
    AvatarSimpleDraweeView mSdvAvatar;
    //昵称
    @Bind(R.id.user_id_nick)
    TextView mTvNick;
    //性别,地址
    @Bind(R.id.user_id_address)
    TextView mTvAddress;
    //粉丝数
    @Bind(R.id.user_id_fans)
    TextView mTvFans;
    //关注数
    @Bind(R.id.user_id_follow)
    TextView mTvFollow;

    //关注
    @Bind(R.id.user_id_follow_btn)
    FollowButton mFbFollow;

    //tablayout
    @Bind(R.id.base_id_appbar_tabs)
    TabLayout mTabLayout;

    //与appbar tab关联的fragments
    private List<FragmentTab> mAppTabs;

    //用户id
    private String mUserId;
    //用户信息
    private User mUser;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mUserId = getIntent().getStringExtra(CstOuer.KEY.USER_ID);
    }

    @Override
    protected void initTop() {
        setNavigation(R.drawable.common_ic_left_nav);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_user);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        //获取用户信息
        getUserInfo();


        Bundle bundle = new Bundle();
        bundle.putString(CstOuer.KEY.USER_ID, mUserId);
        //晒单
        addAppTab(R.string.community_follow, UserFollowFragment.class, bundle);
        //话题
        addAppTab(R.string.home_topic, UserTopicFragment.class, bundle);
        //视频
        //addAppTab(R.string.user_collection_video, UserVideoFragment.class, bundle);

        ViewPager viewPager = (ViewPager)findViewById(R.id.base_id_content_viewpager);
        viewPager.setAdapter(new FragmentTabAdapter(getSupportFragmentManager(),
                mAppTabs));
        mTabLayout.setupWithViewPager(viewPager);

        if(mUserId.equals(OuerApplication.mUser.getUserId())) {
            mFbFollow.setVisibility(View.GONE);
        } else {
            mFbFollow.setVisibility(View.VISIBLE);
        }
    }


    /**
     * 添加appbar上的文本tab
     * @param txtRes
     * @param cls
     */
    public void addAppTab(@StringRes int txtRes, Class<? extends BaseUIFragment> cls) {
        addAppTab(getString(txtRes), cls, null);
    }

    /**
     * 添加appbar上的文本tab
     * @param title
     * @param cls
     */
    public void addAppTab(String title, Class<? extends BaseUIFragment> cls) {
        addAppTab(title, cls, null);
    }

    /**
     * 添加appbar上的文本tab
     * @param txtRes
     * @param cls
     * @param args
     */
    public void addAppTab(@StringRes int txtRes, Class<? extends BaseUIFragment> cls, Bundle args) {
        addAppTab(getString(txtRes), cls, args);
    }

    /**
     * 添加appbar上的文本tab
     * @param title
     * @param cls
     * @param args
     */
    public void addAppTab(String title, Class<? extends BaseUIFragment> cls, Bundle args) {
        if(mTabLayout != null && UtilString.isNotBlank(title)) {
            BaseUIFragment fragment = (BaseUIFragment) Fragment.instantiate(this, cls.getName(), args);
            if(mAppTabs == null) {
                mAppTabs = new ArrayList<>(4);
            }

            FragmentTab tab = new FragmentTab();
            tab.setTitle(title);
            tab.setFragment(fragment);
            mAppTabs.add(tab);
        }
    }


    /**
     * 获取用户信息
     */
    private void getUserInfo() {
        AgnettyFuture future = OuerApplication.mOuerFuture.getUserInfo(mUserId,
                new OuerFutureListener(this){

                    public void onStart(AgnettyResult result) {
                        setLoading(true);
                    }

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        setLoading(false);
                        mUser = (User) result.getAttach();
                        //头像
                        mSdvAvatar.loadAvatar(mUser.getAvatarUrl(), mUser.getLevel());

                        //昵称
                        setTitle(mUser.getNick());
                        mTvNick.setText(mUser.getNick());

                        //性别
                        if(mUser.getSex() == ESex.MALE) {
                            mTvAddress.setCompoundDrawablesWithIntrinsicBounds(R.drawable.user_ic_male, 0, 0, 0);
                        } else {
                            mTvAddress.setCompoundDrawablesWithIntrinsicBounds(R.drawable.user_ic_female, 0, 0, 0);
                        }

                        //地址
                        mTvAddress.setText(mUser.getLocation());
                        //粉丝数
                        mTvFans.setText(Html.fromHtml(getString(R.string.user_fans_count, mUser.getFansCount())));
                        //关注数
                        mTvFollow.setText(Html.fromHtml(getString(R.string.user_follow_count, mUser.getFollowCount())));

                        //关注
                        mFbFollow.follow(mUser.getInterested());
                    }

                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        setRetry(true);
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        setRetry(true);
                    }
                });

        attachDestroyFutures(future);
    }

    @OnClick(R.id.user_id_follow_btn)
    public void follow() {
        mUser.setInterested(!mUser.getInterested());
        mFbFollow.follow(mUser.getInterested());

        if(mUser.getInterested()) {
            OuerApplication.mUser.setFollowCount(OuerApplication.mUser.getFollowCount() + 1);
            mUser.setFansCount(mUser.getFansCount() + 1);
        } else {
            OuerApplication.mUser.setFollowCount(OuerApplication.mUser.getFollowCount() - 1);
            mUser.setFansCount(mUser.getFansCount() - 1);
        }

        mTvFans.setText(Html.fromHtml(getString(R.string.user_fans_count, mUser.getFansCount())));

        //调用关注or取消关注接口
        AgnettyFuture future = OuerApplication.mOuerFuture.follow(
                OuerApplication.mUser.getUserId(),
                mUser.getUserId(),
                mUser.getInterested(), null
        );

        attachDestroyFutures(future);

        //发送关注or取消关注广播
        OuerDispatcher.sendFollowBroadcast(this, mUser.getUserId(),
                mUser.getInterested());
    }

    /**
     *
     * 打开粉丝
     */
    @OnClick(R.id.user_id_fans)
    public void presentFans() {
        OuerDispatcher.presentFanActivity(this, mUserId);
    }

    /**
     * 打开关注
     */
    @OnClick(R.id.user_id_follow)
    public void presentFollow() {
        OuerDispatcher.presentFollowActivity(this, mUserId);
    }
}
